package indi.kider.server.applet.service;

import com.google.common.base.Strings;
import indi.kider.common.constant.AppUserType;
import indi.kider.common.model.apartment.*;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.Tenant;
import indi.kider.server.apartment.mapper.*;
import indi.kider.server.applet.model.User;
import indi.kider.server.employee.mapper.EmployeeMapper;
import indi.kider.server.employee.mapper.TenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@SuppressWarnings("ALL")
@Service
public class LockServiceImpl implements LockService{

    @Autowired
    private DevLockMapper devLockMapper;

    @Autowired
    private DevLockCardMapper devLockCardMapper;
    @Autowired
    private DevLockFingerprintMapper devLockFingerprintMapper;
    @Autowired
    private DevLockVersionMapper devLockVersionMapper;
    @Autowired
    private DevLockPasswordMapper devLockPasswordMapper;

    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public DevLock getLockInfo(String houseId) throws Exception{
        DevLock devLock = new DevLock();
        devLock.setHouseId(Long.valueOf(houseId));
        List<DevLock> list = devLockMapper.select(devLock);
        return list.get(0);
    }

    @Override
    public String delLockInfo(String houseId) throws Exception {
        DevLock devLock = new DevLock();
        devLock.setHouseId(Long.valueOf(houseId));
        return devLockMapper.delete(devLock) > 0 ? "ok":"fail";
    }

    @Override
    public String updateLockInfo(DevLock devLock) throws Exception {
        return devLockMapper.updateByPrimaryKeySelective(devLock) > 0 ? "ok":"fail";
    }


    @Override
    public String addCard(DevLockCard devLockCard, User user) throws Exception {
        Example devLockCardE = new Example(DevLockCard.class);
        if(devLockCard.getCard() == null || Strings.isNullOrEmpty(devLockCard.getCard())){
            throw new Exception("卡信息不能为空！");
        }
        if(devLockCard.getLockId() == null || Strings.isNullOrEmpty(devLockCard.getLockId()) ){
            throw new Exception("锁具不能为空！");
        }
        //租客
        if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){
            Tenant tenant =  tenantMapper.selectByPrimaryKey(user.getId());
            if(devLockCard.getOpenCardRemark() == null){
                devLockCard.setOpenCardRemark("租客自助开卡");
            }
            if(devLockCard.getOpenCardUser() == null){
                devLockCard.setOpenCardUser(tenant.getName());
            }
            devLockCard.setTenantId(user.getId());
            devLockCardE.createCriteria().andEqualTo("tenantId",user.getId()).andEqualTo("lockId",devLockCard.getLockId()).andEqualTo("state",1);
            List<DevLockCard> devLockCardList =  devLockCardMapper.selectByExample(devLockCardE);
            if(devLockCardList != null && !devLockCardList.isEmpty()){
                DevLockCard devLockCardUp = new DevLockCard();
                devLockCardUp.setId(devLockCardList.get(0).getId());
                devLockCardUp.setState(0);
                if(devLockCard.getClearCardRemark() == null){
                    devLockCardUp.setClearCardRemark("租客新开卡注销");
                }
                if(devLockCard.getClearCardUser() == null ){
                    devLockCardUp.setClearCardUser(tenant.getName());
                }
                if(devLockCard.getClearCardTime() == null){
                    devLockCardUp.setClearCardTime(new Date());
                }
                //修改原卡信息
                devLockCardMapper.updateByPrimaryKeySelective(devLockCardUp);
            }
            //房東
        }else if(AppUserType.LANDLORD == user.getType()){
            throw new Exception("暂无权限");
            //工人
        }else if(AppUserType.WORKER == user.getType()){
            throw new Exception("暂无权限");
            //服务人员
        }else if(AppUserType.WAITER == user.getType()){
            throw new Exception("暂无权限");
            //合作企業
        }else if(AppUserType.COOPERATE == user.getType()){
            if(devLockCard.getTenantId() == null){
                throw new Exception("请选择一个租客");
            }else{
                devLockCardE.createCriteria().andEqualTo("tenantId",devLockCard.getTenantId()).andEqualTo("lockId",devLockCard.getLockId()).andEqualTo("state",1);
            }
            Employee employee =  employeeMapper.selectByPrimaryKey(user.getId());
            if(devLockCard.getOpenCardRemark() == null){
                devLockCard.setOpenCardRemark("管理员开卡");
            }
            if(devLockCard.getOpenCardUser() == null){
                devLockCard.setOpenCardUser(employee.getName());
            }
            List<DevLockCard> devLockCardList =  devLockCardMapper.selectByExample(devLockCardE);
            if(devLockCardList != null && !devLockCardList.isEmpty()){
                DevLockCard devLockCardUp = new DevLockCard();
                devLockCardUp.setId(devLockCardList.get(0).getId());
                devLockCardUp.setState(0);
                if(devLockCard.getClearCardRemark() == null){
                    devLockCardUp.setClearCardRemark("管理员开新卡销卡");
                }
                if(devLockCard.getClearCardUser() == null ){
                    devLockCardUp.setClearCardUser(employee.getName());
                }
                if(devLockCard.getClearCardTime() == null){
                    devLockCardUp.setClearCardTime(new Date());
                }
                //修改原卡信息
                devLockCardMapper.updateByPrimaryKeySelective(devLockCardUp);
            }
        }
        //添加一条卡信息
        if(devLockCard.getAddTime() == null){
            devLockCard.setAddTime(new Date());
        }
        if(devLockCard.getOpenCardTime() == null){
            devLockCard.setOpenCardTime(new Date());
        }

        devLockCard.setState(1);
        Integer result = devLockCardMapper.insertSelective(devLockCard);
        return result == 1?"ok":"err";
    }

    @Override
    public List<DevLockCard> getCardInfo(DevLockCard devLockCard, User user) throws Exception {
        List<DevLockCard> devLockCardList = null;
        //租客
        if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){
            devLockCard.setTenantId(user.getId());
            devLockCardList = devLockCardMapper.getCardInfo(devLockCard);
            //房東
        }else if(AppUserType.LANDLORD == user.getType()){
            throw new Exception("暂无权限");
            //工人
        }else if(AppUserType.WORKER == user.getType()){
            throw new Exception("暂无权限");
            //服务人员
        }else if(AppUserType.WAITER == user.getType()){
            throw new Exception("暂无权限");
            //合作企業
        }else if(AppUserType.COOPERATE == user.getType()){
            devLockCard.setEmployeeId(user.getId());
            devLockCardList = devLockCardMapper.getCardInfo(devLockCard);
        }
        return devLockCardList;
    }

    @Override
    public String addFingerprint(DevLockFingerPrint devLockFingerPrint, User user) throws Exception {
        Example devLockFingerPrintE = new Example(DevLockFingerPrint.class);
        /*//租客
        if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){

            //房東
        }else if(AppUserType.LANDLORD == user.getType()){
            throw new Exception("暂无权限");
            //工人
        }else if(AppUserType.WORKER == user.getType()){
            throw new Exception("暂无权限");
            //服务人员
        }else if(AppUserType.WAITER == user.getType()){
            throw new Exception("暂无权限");
            //合作企業
        }else if(AppUserType.COOPERATE == user.getType()){
            throw new Exception("暂无权限");
        }*/
        if(devLockFingerPrint.getTenantId() == null){
            throw new Exception("请选择一个租客！");
        }
        devLockFingerPrintE.createCriteria().andEqualTo("tenantId",devLockFingerPrint.getTenantId()).andEqualTo("state",1);
        List<DevLockFingerPrint> devLockFingerPrintList = devLockFingerprintMapper.selectByExample(devLockFingerPrintE);
        if(devLockFingerPrintList != null && !devLockFingerPrintList.isEmpty()){
            devLockFingerprintMapper.deleteByPrimaryKey(devLockFingerPrintList.get(0).getId());
        }
        //添加一条卡信息
        if(devLockFingerPrint.getAddTime() == null){
            devLockFingerPrint.setAddTime(new Date());
        }
        devLockFingerPrint.setState(1);
        Integer result = devLockFingerprintMapper.insertSelective(devLockFingerPrint);
        return result == 1?"ok":"err";
    }

    @Override
    public List<DevLockFingerPrint> queryFingerPrint(DevLockFingerPrint devLockFingerPrint, User user) throws Exception {
        List<DevLockFingerPrint> devLockFingerPrintList = null;
        //租客
        if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){
            devLockFingerPrint.setTenantId(user.getId());
            devLockFingerPrintList = devLockFingerprintMapper.select(devLockFingerPrint);
            //房東
        }else if(AppUserType.LANDLORD == user.getType()){
            throw new Exception("暂无权限");
            //工人
        }else if(AppUserType.WORKER == user.getType()){
            throw new Exception("暂无权限");
            //服务人员
        }else if(AppUserType.WAITER == user.getType()){
            throw new Exception("暂无权限");
            //合作企業
        }else if(AppUserType.COOPERATE == user.getType()){
            throw new Exception("暂无权限");
        }
        return devLockFingerPrintList;
    }

    @Override
    public String addLockVersionInfo(DevLockVersion devLockVersion, User user) throws Exception {
        Example devLockVersionE = new Example(DevLockVersion.class);
        /*//租客
        if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){

            //房東
        }else if(AppUserType.LANDLORD == user.getType()){
            throw new Exception("暂无权限");
            //工人
        }else if(AppUserType.WORKER == user.getType()){
            throw new Exception("暂无权限");
            //服务人员
        }else if(AppUserType.WAITER == user.getType()){
            throw new Exception("暂无权限");
            //合作企業
        }else if(AppUserType.COOPERATE == user.getType()){
            throw new Exception("暂无权限");
        }*/
        devLockVersionE.createCriteria().andEqualTo("lockType",devLockVersion.getLockType()).andEqualTo("state",1);
        List<DevLockVersion> devLockVersionList = devLockVersionMapper.selectByExample(devLockVersionE);
        if(devLockVersionList != null && !devLockVersionList.isEmpty()){
            DevLockVersion devLockVersionUp = new DevLockVersion();
            devLockVersionUp.setState(0);
            devLockVersionUp.setId(devLockVersionList.get(0).getId());
            devLockVersionMapper.updateByPrimaryKeySelective(devLockVersionUp);
        }
        //添加一条卡信息
        if(devLockVersion.getUpTime() == null){
            devLockVersion.setUpTime(new Date());
        }
        devLockVersion.setState(1);
        Integer result = devLockVersionMapper.insertSelective(devLockVersion);
        return result == 1?"ok":"err";
    }

    @Override
    public List<DevLockVersion> queryLockVersionInfo(DevLockVersion devLockVersion, User user) throws Exception {
        List<DevLockVersion> devLockVersionList = null;
        /*//租客
        if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){
            //房東
        }else if(AppUserType.LANDLORD == user.getType()){
            throw new Exception("暂无权限");
            //工人
        }else if(AppUserType.WORKER == user.getType()){
            throw new Exception("暂无权限");
            //服务人员
        }else if(AppUserType.WAITER == user.getType()){
            throw new Exception("暂无权限");
            //合作企業
        }else if(AppUserType.COOPERATE == user.getType()){
            throw new Exception("暂无权限");
        }*/
        devLockVersionList = devLockVersionMapper.select(devLockVersion);
        return devLockVersionList;
    }

    @Override
    public String addLockPassword(DevLockPassword devLockPassword, User user) throws Exception {
        Example devLockPasswordE = new Example(DevLockPassword.class);
       /* //租客
        if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){

            //房東
        }else if(AppUserType.LANDLORD == user.getType()){
            throw new Exception("暂无权限");
            //工人
        }else if(AppUserType.WORKER == user.getType()){
            throw new Exception("暂无权限");
            //服务人员
        }else if(AppUserType.WAITER == user.getType()){
            throw new Exception("暂无权限");
            //合作企業
        }else if(AppUserType.COOPERATE == user.getType()){
            throw new Exception("暂无权限");
        }*/
        devLockPasswordE.createCriteria().andEqualTo("tenantId",devLockPassword.getTenantId()).andEqualTo("state",1);
        List<DevLockPassword> devLockPasswordList = devLockPasswordMapper.selectByExample(devLockPasswordE);
        if(devLockPasswordList != null && !devLockPasswordList.isEmpty()){
            devLockPasswordMapper.deleteByPrimaryKey(devLockPasswordList.get(0).getId());
        }
        //添加一条卡信息
        if(devLockPassword.getAddTime() == null){
            devLockPassword.setAddTime(new Date());
        }

        devLockPassword.setState(1);
        Integer result = devLockPasswordMapper.insertSelective(devLockPassword);
        return result == 1?"ok":"err";
    }

    @Override
    public List<DevLockPassword> queryLockPassword(DevLockPassword devLockPassword, User user) throws Exception {
        List<DevLockPassword> devLockPasswordList = null;
        //租客
        if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){
            devLockPassword.setTenantId(user.getId());
            devLockPasswordList = devLockPasswordMapper.queryLockPassword(devLockPassword);
            //房東
        }else if(AppUserType.LANDLORD == user.getType()){
            throw new Exception("暂无权限");
            //工人
        }else if(AppUserType.WORKER == user.getType()){
            throw new Exception("暂无权限");
            //服务人员
        }else if(AppUserType.WAITER == user.getType()){
            throw new Exception("暂无权限");
            //合作企業
        }else if(AppUserType.COOPERATE == user.getType()){
            devLockPassword.setEmployeeId(user.getId());
            devLockPasswordList = devLockPasswordMapper.queryLockPassword(devLockPassword);
        }
        return devLockPasswordList;
    }

    @Override
    public String delFingerprint(DevLockFingerPrint devLockFingerPrint, User user) {
        int i = devLockFingerprintMapper.delete(devLockFingerPrint);
        return i > 0 ?"ok":"err";
    }

    @Override
    public String delLockPassword(DevLockPassword devLockPassword, User user) {
        int i = devLockPasswordMapper.delete(devLockPassword);
        return i > 0 ?"ok":"err";
    }

    @Override
    public String delCard(DevLockCard devLockCard, User user)  throws Exception{
        int result = 0;
        Example devLockCardE = new Example(DevLockCard.class);
        if(!Strings.isNullOrEmpty(devLockCard.getLockId())&&devLockCard.getTenantId()!=null){
            devLockCardE.createCriteria().andEqualTo("tenantId",user.getId()).andEqualTo("lockId",devLockCard.getLockId()).andEqualTo("state",1);
        }
        if(devLockCard.getId() != null){
            devLockCardE.createCriteria().andEqualTo("id",devLockCard.getId()).andEqualTo("state",1);
        }
        Employee employee = null;
        if(devLockCard.getClearCardUser() == null ){
            employee = employeeMapper.selectByPrimaryKey(user.getId());
        }

        List<DevLockCard> devLockCardList =  devLockCardMapper.selectByExample(devLockCardE);
        if(devLockCardList != null && !devLockCardList.isEmpty()){
            DevLockCard devLockCardUp = new DevLockCard();
            devLockCardUp.setId(devLockCardList.get(0).getId());
            devLockCardUp.setState(0);
            if(devLockCard.getClearCardRemark() == null){
                devLockCardUp.setClearCardRemark("管理员销卡");
            }else{
                devLockCardUp.setClearCardRemark(devLockCard.getClearCardRemark());
            }
            if(devLockCard.getClearCardUser() == null ){
                devLockCardUp.setClearCardUser(employee.getName());
            }else{
                devLockCardUp.setClearCardUser(devLockCard.getClearCardUser());
            }
            if(devLockCard.getClearCardTime() == null){
                devLockCardUp.setClearCardTime(new Date());
            }else{
                devLockCardUp.setClearCardTime(devLockCard.getClearCardTime());
            }
            //修改原卡信息
            result = devLockCardMapper.updateByPrimaryKeySelective(devLockCardUp);
            return result == 1?"ok":"err";
        }else{
            throw new Exception("没有对应的卡信息");
        }
    }
}
